package com.yauchu.kegra.graph;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.jgrapht.DirectedGraph;
import org.jgrapht.graph.DefaultDirectedGraph;

import android.widget.LinearLayout;

import com.yauchu.kegra.graph.traversers.ExpressionGenerator;
import com.yauchu.kegra.graph.traversers.GraphViewGenerator;
import com.yauchu.kegra.graph.traversers.RefreshTraverser;

public class GraphContainer {
	// private ImageView RootFormula;
	// private ImageView LeftFormula;
	// private ImageView RightFormula;
	//private Context context;
	// private DirectedGraph<GraphNode,DefaultEdge> structure=new
	// DefaultDirectedGraph<GraphNode,DefaultEdge>(DefaultEdge.class);
	private DirectedGraph<GraphNode, NumberedEdge> structure = new DefaultDirectedGraph<GraphNode, NumberedEdge>(
			NumberedEdge.class);
	
	
	

	
	//private LinearLayout parentLayout;
	// private Bitmap b;
	// private Bitmap p;
	// private Bitmap defa;

	private GraphNode rootNode;
	//private GraphNode leftNode;
	//private GraphNode rightNode;
	private GraphViewGenerator graphViewGenerator=new GraphViewGenerator(structure);
	private ExpressionGenerator expressionGenerator= new ExpressionGenerator(structure);
	private RefreshTraverser refreshTraverser = new RefreshTraverser(structure);
	
	public GraphContainer(GraphNode rootNode) {
		//this.context = context;
		this.rootNode = rootNode;
		rootNode.graphContainerInitialization(this);
		rootNode.turnOnDragEventListeners();
		//this.parentLayout=parentLayout;
		//this.leftNode = leftNode;
		//this.rightNode = rightNode;

		

		structure.addVertex(rootNode);
		//structure.addVertex(leftNode);
		//structure.addVertex(rightNode);

		//structure.addEdge(rootNode, leftNode,new NumberedEdge(rootNode,leftNode,"1"));
		//structure.addEdge(rootNode, rightNode,new NumberedEdge(rootNode,rightNode,"2"));

	}

	public void AddNode(GraphNode where,GraphNode what,int argumentNumber)
	
	{	
		what.graphContainerInitialization(this);
		what.switchToColorCodedRepresentation();
		
		structure.addVertex(what);
		what.turnOnDragEventListeners();
		structure.addEdge(where, what, new NumberedEdge(where,what,String.valueOf(argumentNumber)));
		
		
	}
	/*
	private LinearLayout generateGraphView(GraphNode node, int depth) {
		LinearLayout mainView = new LinearLayout(context);
		
		mainView.setOrientation(LinearLayout.VERTICAL);
		
		mainView.addView(node.generateImageView());
		
		if (depth != 0) {

			Set<NumberedEdge> edgeset = structure.outgoingEdgesOf(node);
			
			List<NumberedEdge> edgelist=new ArrayList(edgeset);
			
			Collections.sort(edgelist);
			
			Iterator<NumberedEdge> iteredge = edgelist.iterator();

			
			
			List<GraphNode> lgr=new ArrayList<GraphNode>();
			
			NumberedEdge currentedge;
			
			while (iteredge.hasNext())
			{	
				currentedge= iteredge.next();
				
				GraphNode what= currentedge.getV2();
				//int where=Integer.valueOf(currentedge.toString());
				lgr.add(what);
				
			
				//lgr.add(where,what);
				
			}
			Iterator<GraphNode> iternode=lgr.iterator();
					
			LinearLayout newLayout= new LinearLayout(mainView.getContext());
			newLayout.setOrientation(LinearLayout.HORIZONTAL);
			mainView.addView(newLayout);
			
			while(iternode.hasNext()){
				
				newLayout.addView(generateGraphView(iternode.next(),depth -1));
				
			}


			
			// Make recursive View generating function

		}
		return mainView;
	}
	
	*/
	
	public Iterator<String> getArguments(GraphNode node)
	{
		Set<NumberedEdge> edgeset = structure.outgoingEdgesOf(node);
		
		List<NumberedEdge> edgelist=new ArrayList(edgeset);
		
		Collections.sort(edgelist);
		
		Iterator<NumberedEdge> iteredge = edgelist.iterator();

		
		
		ArrayList<String> strArray=new ArrayList<String>();
		
		//NumberedEdge currentedge;
		
		while (iteredge.hasNext())
		{
			strArray.add(iteredge.next().getV2().getFullStringRepresentation());
		}
		
		return strArray.iterator();
			
	}
	
	public void refreshUpFromThis(GraphNode node)
	{
		//TODO make calculation of depth
		refreshTraverser.refreshOnDepth(rootNode, 2);
		
	}
	
	public LinearLayout generateView(GraphNode rootNode,int  depth)
	
	{
		// LinearLayout mainView=generateGraphView(rootNode,depth);		

		
		return graphViewGenerator.generateView(rootNode, depth);
		
		
	}

	public String getStringRepresentation() {
		//return rootNode.getStringRepresentation();
		
		return expressionGenerator.getExpression(rootNode,3);
	}


}
